package frameworks.widgets
{
	import flash.utils.Dictionary;
	
	public class BaseSender
	{
		public var levelID:uint = NaN;
		private var _manager:SenderManager;
		private var _propertys:Object = {"*":1};
		private var _propertyAccess:Object = {"*":1};
		private var _propertysChanged:Object = {};
		private var _propertysChangedTemp:Object = _propertysChanged;
		private var _propertyWatchers:Object = {};//{"x":{awatchers:{fun1:"true",fun2:"true"},bwatchers:{fun3:"true",fun4:"true"},...},"y":{awatchers:{fun5:"true"},...}...}
		
		public function BaseSender(manager:SenderManager)
		{
			_manager = manager;
		}
		public function get changeList():Object{
			return _propertysChangedTemp;
		}
		public function get changed():Boolean{
			return (_propertysChangedTemp["*"]===true);
		}
		public function changedProperty(property:String):Boolean{
			return _propertysChangedTemp[property]!=null;
		}
		public function getPropertyAccess(property:String):int{
			return _propertyAccess[property];
		}
		protected function regProperty(property:String, value:*, access:int=-1):void{
			if(_propertys[property] !== undefined){throw Error(property+"属性已经存在！");return;}
			if(access === 0 && !(value is BaseSender)){throw Error(property+"锁定的属性必须为Sender！");return;}
			_propertyAccess[property] = access;
			_propertys[property] = value;
		}
		
		protected function getProperty(property:String):*{
			return _propertys[property];
		}
		protected function setProperty(property:String, value:*):Boolean{
			if(_propertyAccess[property] !== -1 && _propertys[property] !== value){
				if((value is Number) && isNaN(value) && isNaN(_propertys[property])){return false;}
				if(_propertyAccess[property] === 0){
					BaseSender(_propertys[property]).setPropertysSource(value);
				}else{
					_propertys[property] = value;
					_propertysChanged[property] = true;
					if(!_propertysChanged["*"]){
						_propertysChanged["*"] = true;
						_manager.senderChanged(this);
					}
				}
				return true;
			}else{
				return false;
			}
		}
		public function setPropertysSource(data:Object):void{
			for(var property:String in _propertyAccess){
				if(_propertyAccess[property]!==-1 && data.hasOwnProperty(property)){
					setProperty(property, data[property]);
				}
			}
		}
		
		public function preVerify():void{
			trace("afterSubmit",this);
		}
		public function frontVerify():void{
			trace("frontSubmit",this);
		}
		public function backVerify():void{
			trace("backSubmit",this);
		}
		internal function sumbitChangedFuns(changedFuns:Dictionary):void{
			for(var property:String in _propertysChanged){
				for each(var item:Object in _propertyWatchers[property]){
					for(var fun:Object in item){
						changedFuns[fun] = false;//false表示非初次请求绑定数据
					}
				}
				
			}
			_propertysChanged = {};//将_propertysChanged初始化，以接收sender过程中新的change，原list由_propertysChangedTemp保持
		}
		internal function senderAllComplete():void{
			_propertysChangedTemp = _propertysChanged;//将changed list交由_propertysChangedTemp共同引用
		}
		public function addWatcher(watchers:Watchers, property:String, fun:Function):void{
			if(property == ""){property = "*";}
			if(_propertyAccess[property] === 1){
				watchers.senderAdded(this, property, fun);
				watchersAdded(watchers, property, fun);
				_manager.addInitWatcherFunction(fun);
			}else{
				throw Error(property+"属性不允许改变！无效的侦听！");
			}
			
		}
		internal function watchersAdded(watchers:Watchers, property:String, fun:Function):void{
			_manager.regWatcherFunction(fun);
			if(!_propertyWatchers[property]){
				_propertyWatchers[property] = new Dictionary();
			}
			if(!_propertyWatchers[property][watchers]){
				_propertyWatchers[property][watchers] = new Dictionary();
			}
			if(!_propertyWatchers[property][watchers][fun]){
				_propertyWatchers[property][watchers][fun]  = true;
			}
		}
		
	}
}